import { PaginatorPlugin } from "@datorama/akita";
import { switchMap } from "rxjs/operators";
import { AnchorFollowQuery, AnchorFollowStore, makeStore, AnchorFollowEntity } from "@/state/anchorFollowEntity/store";
import authService from "@/services/authService";

export class RoomsFacade {
  constructor(private store: AnchorFollowStore, private query: AnchorFollowQuery, public paginator: PaginatorPlugin<AnchorFollowEntity>) {

  }
  readonly filter$ = this.query.filter$;
  pagination$ = this.paginator.pageChanges.pipe(
    //获取关注的主播列表数据 
    switchMap((pageNum) => {
      const reqFn = () => authService.getAnchorsSvc(pageNum, 10)
      return this.paginator.getPage(reqFn);
    }),
  )

  prevPage = () => {
    this.paginator.prevPage();
  }

  nextPage = () => {
    this.paginator.nextPage();
  }

  //销毁store实例，释放内存
  destroy = () => {
    this.paginator.destroy();
    this.store.destroy();
  }
}

export const makeFacade = (): RoomsFacade => {
  const [store, query, rooms_paginator] = makeStore();

  return new RoomsFacade(store, query, rooms_paginator);
}